<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * Tables
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/tableenv/index
 */
class Tableenv extends CsmBackend
{

    protected $model = null;

    protected $relationSearch = true;

    /**
     * 查看
     */
    public function index()
    {
        CsmonlineCron::calcuserlog();
        CsmonlineCron::generatefauser();

        if ($this->request->isAjax()) {
            list ($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $begin = $this->csmreq("begin", true);
            $end = $this->csmreq("end", true);

            $dao = new \app\admin\model\csmonline\Pieevn();
            $list = $dao->alias('t')
                ->where("statdate", ">=", $begin)
                ->where("statdate", "<=", $end)
                ->where($where)
                ->order("ipsy", "desc")
                ->field("envvalue,sum(ipsy) ipsy,sum(pvsy) pvsy,sum(uvsy) uvsy,sum(vvsy) vvsy,sum(ipsn) ipsn,sum(pvsn) pvsn,sum(uvsn) uvsn,sum(vvsn) vvsn")
                ->group("envvalue")
                ->select();

            $trow = $dao->alias('t')
                ->where("statdate", ">=", $begin)
                ->where("statdate", "<=", $end)
                ->where($where)
                ->order("ipsy", "desc")
                ->field("sum(ipsy) ipsy,sum(pvsy) pvsy,sum(uvsy) uvsy,sum(vvsy) vvsy,sum(ipsn) ipsn,sum(pvsn) pvsn,sum(uvsn) uvsn,sum(vvsn) vvsn")
                ->find();
            foreach ($list as $k => $row) {
                $row['tipsy'] = $this->_calcpercent($row, $trow, 'ipsy');
                $row['tpvsy'] = $this->_calcpercent($row, $trow, 'pvsy');
                $row['tuvsy'] = $this->_calcpercent($row, $trow, 'uvsy');
                $row['tvvsy'] = $this->_calcpercent($row, $trow, 'vvsy');

                $row['tipsn'] = $this->_calcpercent($row, $trow, 'ipsn');
                $row['tpvsn'] = $this->_calcpercent($row, $trow, 'pvsn');
                $row['tuvsn'] = $this->_calcpercent($row, $trow, 'uvsn');
                $row['tvvsn'] = $this->_calcpercent($row, $trow, 'vvsn');
                $list[$k] = $row;
            }

            // $this->tracedao($dao);
            $result = array(
                "total" => count($list),
                "rows" => $list
            );
            return json($result);
        }
        return $this->view->fetch();
    }

    private function _calcpercent($row, $totalrow, $keyname)
    {
        $fz = $row->$keyname;
        $fm = $totalrow->$keyname;
        if ($fm == 0) {
            return 0;
        } else {
            return round($fz / $fm, 3) * 100;
        }
    }
}